package com.example.seckill.Controller;

import com.example.seckill.Exception.ErrorEnum;
import com.example.seckill.Exception.GlobalException;
import com.example.seckill.VO.BuildingVO;
import com.example.seckill.common.CommonResult;
import com.example.seckill.mapper.BuildingMapper;
import com.example.seckill.mapper.FacilityMapper;
import com.example.seckill.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    BuildingMapper buildingMapper;

    @Autowired
    FacilityMapper facilityMapper;

    @Autowired
    RoomMapper roomMapper;

    @Autowired
    RedisTemplate redisTemplate;


    @GetMapping("getAllBuildings")
    public CommonResult<List<BuildingVO>> GetBuildingList(){
        //在redis中查询
        String key="BuildingList";
        //不用存在redis中
//        List<BuildingVO> res=(List<BuildingVO>)redisTemplate.opsForValue().get(key);
//        if(res==null){
        List<BuildingVO> res;
        res=buildingMapper.selectAllBuildings();
        if(res==null) throw new GlobalException(ErrorEnum.NO_BUILDING_FOUND);
        for (BuildingVO b:res){
            Integer bid=b.getBuilding_id();
            b.setBuilding_gender(buildingMapper.getBuildingGender(bid));
        }
        redisTemplate.opsForValue().set(key,res);
//        }

        return CommonResult.success(res);
    }
//    getBuildingByBID 宿舍楼详情
    @GetMapping("getBuildingByBID/{bid}")
    public CommonResult<BuildingVO> getBuildingByBID(@PathVariable Integer bid){
        BuildingVO res;
        res = buildingMapper.selectByBuildingID(bid);
        if (res == null) throw new GlobalException(ErrorEnum.NO_BUILDING_FOUND);
        res.setFacilityList(facilityMapper.getFacilityByBID(bid));
        res.setBuilding_gender(buildingMapper.getBuildingGender(bid));
        res.setBuilding_roomType(buildingMapper.getBuildingRoomType(bid));
        return CommonResult.success(res);
    }

//    @Override
//    //redis预存
//    public void afterPropertiesSet() throws Exception {
//        //缓存buildingList
//        List<BuildingVO> buildingList=buildingMapper.selectAllBuildings();
//        if(buildingList==null) return ;
//
//        redisTemplate.opsForValue().set("BuildingList",buildingList);
//
//
//        buildingList.forEach(buildingVO -> {
//
//            Integer bid=buildingVO.getBuilding_id();
//            buildingVO.setBuilding_gender(buildingMapper.getBuildingGender(bid));
//            buildingVO.setFacilityList(facilityMapper.getFacilityByBID(bid));
//            buildingVO.setBuilding_roomType(buildingMapper.getBuildingRoomType(bid));
//            redisTemplate.opsForValue().set("BuildingInfo",buildingVO);
//
//        });
//    }
//    @GetMapping("/picture")
//    public CommonResult<BufferedImage> showPic() throws IOException {
////        int buildingId = buildingRequest.getBuilding_id();
////        Building foundBuilding=buildingMapper.selectByBuildingID(buildingId);
////        if(foundBuilding!=null) {
////            String imgPath = foundBuilding.getBuilding_image();
//        String imgPath="C:\\Users\\nuage\\Desktop\\在线选宿舍\\sec-kill\\src\\main\\resources\\pictures\\b5.jpg";
//            BufferedImage picture = ImageIO.read(new FileInputStream(imgPath));
//            return CommonResult.success(picture);

//        }
//        else return CommonResult.error(400,"错误");
//        response.setContentType("image/jpg");
//        try (OutputStream outputStream = response.getOutputStream()) {
//            ImageIO.write(picture, "jpg", outputStream);
//            outputStream.flush();
//        }




}
